<html>
<head><meta charset="utf-8"><title>Rust-analyzer - Modifier for callable variables · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html">Rust-analyzer - Modifier for callable variables</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213492421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492421">(Oct 15 2020 at 21:49)</a>:</h4>
<p>I wanted to add a modifier for variables that are callable for a while now. Motivated by when creating a fn pointer or a closure the variable name in call expression not being colorized.</p>
<p>For plain fn pointer and closures I have something kinda working on a local branch</p>
<p>I have 2 questions:</p>
<ol>
<li>Is that a wanted feature?</li>
<li>How do I check if a variable is of a type that implements the Fn{Once, Mut} traits to properly add the modifier for parameter generic over those traits?</li>
</ol>



<a name="213492665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492665">(Oct 15 2020 at 21:51)</a>:</h4>
<p>Seems reasonable</p>



<a name="213492676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492676">(Oct 15 2020 at 21:51)</a>:</h4>
<p>Do you need a modifier though?</p>



<a name="213492692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492692">(Oct 15 2020 at 21:52)</a>:</h4>
<p>Can't you just use "Function" tag?</p>



<a name="213492753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492753">(Oct 15 2020 at 21:52)</a>:</h4>
<p>Hm, I guess function and local would be in conflict, and a modifer might be indeed better...</p>



<a name="213492851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492851">(Oct 15 2020 at 21:53)</a>:</h4>
<p>You can use this method to determine if a type is a callable or not: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir/src/code_model.rs#L1450">https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir/src/code_model.rs#L1450</a></p>



<a name="213492903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213492903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213492903">(Oct 15 2020 at 21:53)</a>:</h4>
<p>And I think other code in highlighting already fetches types for locals to underly &amp;mut</p>



<a name="213493571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213493571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213493571">(Oct 15 2020 at 22:01)</a>:</h4>
<p>Thanks a bunch!! I'll send a PR as soon as I can</p>



<a name="213533829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213533829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213533829">(Oct 16 2020 at 08:48)</a>:</h4>
<p>It seems that as_callable only returns Some in case of FnDef, Closure and FnPtr</p>



<a name="213533957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213533957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213533957">(Oct 16 2020 at 08:50)</a>:</h4>
<p>In the case of generics of Fn traits as_callable returns None</p>



<a name="213534033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213534033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213534033">(Oct 16 2020 at 08:51)</a>:</h4>
<p>So how do I check if a Type implements FnOnce/FnMut/Fn?</p>



<a name="213537264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213537264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213537264">(Oct 16 2020 at 09:25)</a>:</h4>
<p>Hm, I think thats a bug</p>



<a name="213537274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213537274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213537274">(Oct 16 2020 at 09:25)</a>:</h4>
<p>the thing should return <code>Some</code> for generics</p>



<a name="213537934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213537934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213537934">(Oct 16 2020 at 09:33)</a>:</h4>
<p>the function "callable_sig" returns Some when TypeCtor::FnPtr, TypeCtor::FnDef and TypeCtor::Closure, but I'm not exactly comfortable with the code of "callable_sig", so maybe I'm the one missing something</p>



<a name="213538312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213538312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213538312">(Oct 16 2020 at 09:38)</a>:</h4>
<p>Hm, yeah, it feels like that should also handle Ty::PLaceholder</p>



<a name="213538330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213538330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213538330">(Oct 16 2020 at 09:38)</a>:</h4>
<p><span class="user-mention" data-user-id="129457">@Florian Diebold</span> might be the right person to ask this :-)</p>



<a name="213540173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213540173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213540173">(Oct 16 2020 at 09:58)</a>:</h4>
<p>Fn traits aren't handled by <code>callable_sig</code> because they need some more involved handling during <a href="https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir_ty/src/infer/expr.rs#L67">inference</a>. A function with the signature of <code>Ty::callable_sig</code> can't actually check for the Fn trait because it needs the environment (also note that the expected number of args is actually relevant for inference). I think this needs separate and slightly different handling outside of inference, similar to that function and to the way we check for <a href="https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir/src/code_model.rs#L1366"><code>Future</code></a>, for example. I'd be a bit worried about doing so many trait solves, but if you just do it for type parameters it'd probably be ok</p>



<a name="213540382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213540382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213540382">(Oct 16 2020 at 10:01)</a>:</h4>
<p>(and I don't think a helper method on <code>Ty</code> should be doing any trait solving in general)</p>



<a name="213540895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213540895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213540895">(Oct 16 2020 at 10:07)</a>:</h4>
<p>The code to check if implements Fn traits is somewhat similar to the code in impls_future?<br>
If yes what is the string I have to pass to <a href="https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir/src/code_model.rs#L1372"><code>lang_item</code></a>?</p>



<a name="213541185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213541185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213541185">(Oct 16 2020 at 10:11)</a>:</h4>
<p><code>FnTrait::FnOnce.lang_item_name()</code></p>



<a name="213541194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213541194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213541194">(Oct 16 2020 at 10:11)</a>:</h4>
<p>i.e. <code>"fn_once"</code></p>



<a name="213541942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213541942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213541942">(Oct 16 2020 at 10:21)</a>:</h4>
<p>Should I make lang_item_name public or just use "fn_once"?</p>



<a name="213542213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213542213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213542213">(Oct 16 2020 at 10:25)</a>:</h4>
<p>actually, you could just use <code>get_id</code></p>



<a name="213542646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer%20-%20Modifier%20for%20callable%20variables/near/213542646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gray Jack <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rust-analyzer.20-.20Modifier.20for.20callable.20variables.html#213542646">(Oct 16 2020 at 10:31)</a>:</h4>
<p>True, I missed that</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>